package bean;

import java.util.ArrayList;
import java.util.List;

import dao.impl.StaticData;
import exception.DBException;

public class Segment {
	public static List<Segment> segs;
	private List<Point> points;
	private int id;
	private StaticData staticData;

	public StaticData getStaticData() {
		return staticData;
	}

	public void setStaticData(StaticData staticData) {
		this.staticData = staticData;
	}

	public Segment() {

	}

	public Segment(int area) {
		if (segs == null) {
			calSegment(area);
		}
		this.points = new ArrayList<Point>();
	}

	public List<Point> getPoints() {
		return points;
	}

	public void setPoints(List<Point> points) {
		this.points = points;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void addPoint(Point p) {
		this.points.add(p);
	}

	public  List<Segment> calSegment(int area) throws DBException {
		List<Point> allPoints = staticData.getAllPoints(area);
		List<Point> points = null;
		Segment temp = null;
		int id = 1;
		for (Point ps : allPoints) {
			if (ps != null) {
				if (segs == null) {
					segs = new ArrayList<Segment>();
					temp = new Segment(area);
					temp.setId(id);
					id++;
					temp.addPoint(ps);
					segs.add(temp);
				} else {
					for (int i = 0; i < segs.size(); i++) {
						Segment seg = segs.get(i);
						if (seg != null) {
							points = seg.getPoints();
							if (!inIntersection(points.get(points.size()-1))&&
									((ps.getX() == points.get(points.size() - 1).getX() && points.size() - 2 < 0)|| 
									(ps.getX() == points.get(points.size() - 1).getX() && ps.getX() == points.get(points.size() - 2).getX()))) {
								points.add(ps);
								seg.setPoints(points);
							} else if (!inIntersection(points.get(points.size()-1))&&
									((ps.getY() == points.get(points.size() - 1).getY() && points.size() - 2 < 0)||
										(ps.getY() == points.get(points.size() - 1).getY()&& ps.getY() == points.get(points.size() - 2).getY()))){
								points.add(ps);
								seg.setPoints(points);
							} else {
								temp = new Segment(area);
								temp.setId(id);
								id++;
								temp.addPoint(ps);
								segs.add(temp);
							}
						}
					} // end for
				} // end if else
			} // end if
		} // end for
		return segs;
	}

	public static boolean inIntersection(Point p) {
		List<Intersection> intersections = StaticData.allIntersection;
		for (Intersection in : intersections) {
			if ((p.getX() == in.getE1().getX() && p.getY() == in.getE1().getY())
					|| (p.getX() == in.getE2().getX() && p.getY() == in.getE2()
							.getY())
					|| (p.getX() == in.getW1().getX() && p.getY() == in.getW1()
							.getY())
					|| (p.getX() == in.getW2().getX() && p.getY() == in.getW2()
							.getY())
					|| (p.getX() == in.getN1().getX() && p.getY() == in.getN1()
							.getY())
					|| (p.getX() == in.getN2().getX() && p.getY() == in.getN2()
							.getY())
					|| (p.getX() == in.getS1().getX() && p.getY() == in.getS1()
							.getY())
					|| (p.getX() == in.getS2().getX() && p.getY() == in.getS2()
							.getY())) {
				return true;
			}
		}
		return false;
	}
}
